/**
 * 发票
 * @author liuw
 * @date 2019/09/04
 */
define(function () {
    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            { field: "GWEFDOCNO", width: 150, title: "开票单号" ,sort:true,search:'default',searchOpts:{icon:true}},
            { field: "GWEFSEQ", width: 100, title: "项次" },
            { field: "GWEF001", width: 150, title: "开票申请单号",sort:true },
            { field: "GWEFDOCDATE", width: 100, title: "开票日期",
                search:'default',searchOpts:{type:'date'},sort:true },
            { field: "GWEF002", width: 100,hide:true,title:"客户",
                search:'default',searchOpts:{icon:true} }, // 客户编号
            { field: "GWEF002_NAME", width: 100, title: "客户" },
            { field: "GWEF003", width: 180, title: "出货单号",sort:true },
            { field: "GWED102", width: 100, title: "出货单项次"},
            { field: "GWEF004", width: 150, title: "订单号" ,sort:true},
            { field: "GWEC101", width: 100, title: "订单项次" },
            { field: "GWEF006", width: 100, title: "币种",hide:true },
            { field: "GWEF007", width: 100, title: "汇率",hide:true },
            { field: "GWEF008", width: 100, title: "产品编号" },
            { field: "GWAML003", width: 200, title: "品名" },
            { field: "GWAML004", width: 100, title: "规格" },
            { field: "GWEF009", width: 100, title: "数量",sort:true },
            { field: "GWEF010", width: 100, title: "原币单价",sort:true },
            { field: "GWEF011", width: 120, title: "原币含税金额",sort:true },
            { field: "GWEF012", width: 100, title: "原币税额"},
            { field: "GWEF013", width: 120, title: "原币不含税金额",sort:true },
            { field: "GWEF014", width: 100, title: "本币单价",sort:true},
            { field: "GWEF015", width: 100, title: "含税金额",sort:true},
            { field: "GWEF016", width: 100, title: "税额" },
            { field: "GWEF017", width: 100, title: "不含税金额" ,sort:true},
            { field: "GWEFCONF", width: 100, title: "状态",
                filterDatas: app.datas_('crm_gwcd_gwcdacti')},
            { field: "GWEFACTI", width: 100, title: "有效否",filterDatas: app.datas_('sys_y_n') },
            {
                width: 60,
                title: '',
                toolbar: '#script-gwei007setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwei007SettingAction,
        moduleName = '发票'
    ;

    app.newModule({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwei007',
                _searchs: [
                    {
                        id: 'gwei007',
                        tableId: 'gwei007_table',
                        datas:  _searchsArr.pushArray([
                            { field: 'GWEFCONF', title: '状态',filterDatas: app.datas_('crm_gwei006_conf'),
                                search: 'default', searchOpts:{type:'select'}}
                        ])
                    }
                ],
                _loadTplsFull: [
                    {
                        dom: 'gwei007_edit', tpl: 'gwei007_edit'
                    },{
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwei007_table'),
                    url: g_ctxPath + 'crm/gwei007/getList',
                    exportsUrl: g_ctxPath + 'crm/gwei007/getGwcslist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwei007_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: ''
                });

                layui.laytpl(app.getByid(doc, 'script-gwei007setting-action').innerHTML).render({}, function (string) {
                    gwei007SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwei007_table)', function (obj) {

                    layer.tips(gwei007SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwei007_edit',
                                            tagData:{
                                                moduleName:moduleName,
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                minmax:true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWEFCONF === 'Y' ){
                                            app.showWarning("已审核的数据不能删除");
                                            return ;
                                        }
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwei007_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwei007_edit',
                        tagData: {
                            moduleName:moduleName,
                            obj:obj
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '详情',
                            minmax:true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwei007_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwei007_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    minmax:true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'check':
                            self.checkDataBatch();
                            break;
                        case 'unCheck':
                            self.unCheckDataBatch();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwei007_table', where: table_param});
        },

        delData: function (obj) {
            if(obj.data.GWEFCONF == 'Y') {
                app.showWarning("已审核的数据无法删除！");
                return;
            }
            app.ajax({
                xurl: 'crm/gwei007/del',
                xdata: obj.data,
                loading: true,
                confirm: {msg: "是否删除选择的数据？"},
                okmsg: moduleName + '删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwei007_table');

            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = {};
                    if(v.GWEFCONF == 'Y' ){
                        app.showWarning("存在已审核的数据，无法删除！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEFDOCNO = v.GWEFDOCNO;
                    _temp.GWEFSEQ = v.GWEFSEQ;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei007/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否删除选择的数据？"},
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            } else {
                app.showWarning("至少选择一条数据进行操作");
            }
        },

        checkDataBatch: function(){
            var checkStatus = table.checkStatus('gwei007_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = {};
                    if(v.GWEFCONF == 'Y' ){
                        app.showWarning("存在已审核的数据，未审核的数据才能审核！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEFDOCNO = v.GWEFDOCNO;
                    _temp.GWEFSEQ = v.GWEFSEQ;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei007/checkDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否审核选择的数据？"},
                        okmsg: moduleName + '信息审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            } else {
                app.showWarning("至少选择一条数据进行操作");
            }
        },

        unCheckDataBatch: function(){
            var checkStatus = table.checkStatus('gwei007_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = {};
                    if(v.GWEFCONF == 'N' ){
                        app.showWarning("存在未审核的数据，已审核的数据才能取消审核！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEFDOCNO = v.GWEFDOCNO;
                    _temp.GWEFSEQ = v.GWEFSEQ;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei007/cancelCheckDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否取消审核选择的数据？"},
                        okmsg: moduleName + '信息取消审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            } else {
                app.showWarning("至少选择一条数据进行操作");
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwei007',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWEFDOCNO') {//单号开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwefdocno',
                                            columnList: [
                                                {label: '开票单号', prop: 'GWEFDOCNO', width: '150'},
                                                {label: '开票日期', prop: 'GWEFDOCDATE', width: '150'},
                                                {label: '发票申请单号', prop: 'GWEF001', width: '150'},
                                                {label: '状态', prop: 'GWEFCONF', width: '150',sortable:false}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEFDOCNO = data.GWEFDOCNO;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择开票单号',
                                        area: ['630px', '465px']
                                    }
                                });
                            } else if (field == 'GWEF002') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label:'客户编号',prop:'GWAK001', width: 150},
                                                {label:'客户简称',prop:'GWAKL003', width: 150},
                                                {label:'客户全称',prop:'GWAKL004', width: 150}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEF002 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});